/*
 * FileRenamer.java
 *
 * Created on 24 aprile 2007, 11.21
 */

package alecava;

import alecava.interfacce.Updatable;
import java.util.*;
import java.io.*;
import javax.swing.JOptionPane;
import javax.swing.ListModel;
/**
 *
 * @author  alecava
 */
public class FileRenamer extends javax.swing.JFrame {
    private Collection<AbstractCavaID3> files;
    private Updatable<AbstractCavaID3> u;
    private final String lastEspr = "espressioneRinomina";
    /** Creates new form FileRenamer */
    public FileRenamer(Vector<AbstractCavaID3> v, Updatable<AbstractCavaID3> u) {
        initComponents();
        this.files = v;
        this.u = u;
        /*inizializza la lista*/
        lista.setListData(v);
        /*visualizza*/
        this.setVisible(true);
        /*imposta le info*/
        textInfo.setContentType("text/html");
        textInfo.setText("<html><font size=\"3\">E' possibile definire delle espressioni che permettono una rinominazione automatica dei file." +
                "<br/>Gli operatori concessi sono <b>&#060;artista&#062;</b>, <b>&#060;titolo&#062;</b>, <b>&#060;album&#062;</b>, <b>&#060;commento&#062;</b>, <b>&#060;traccia&#062;</b>, <b>&#060;anno&#062;</b>, <b>&#060;genere&#062;</b>." +
                "<br/><i>Esempio: &#060;traccia&#062; - &#060;artista&#062; - &#060;titolo&#062;</i>" +
                "<br/>E' possibile sostituire alle parentesi angolate (&#60 &#62) i simboli di operatore condizionale (? !) in tal caso" +
                " il tag verrà inserito nel nome del file solo se esiste"+
                "<br/>Esempio: ?traccia! - &#60artista&#62, il numero di traccia (col separatore che lo segue) verrà inserito solo se diverso da zero" +
                "<br/><i>Nota: alcuni caratteri non possono essere usati nel nome file, se verranno usati la rinominazione fallirà</i></font></html>");
        /*ricarica l'espressione se esiste*/
        textEspressione.setText((String) MemoryCard.carica(lastEspr));
        //aggiorna l'anteprima
        this.aggiornaAnteprima();
    }
    
    
    
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:initComponents
    private void initComponents() {
        jLabel1 = new javax.swing.JLabel();
        textEspressione = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        labelAnteprima = new javax.swing.JLabel();
        btnRinomina = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        lista = new javax.swing.JList();
        jLabel3 = new javax.swing.JLabel();
        jScrollPane2 = new javax.swing.JScrollPane();
        textInfo = new javax.swing.JTextPane();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Rinomina files");
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowClosing(java.awt.event.WindowEvent evt) {
                formWindowClosing(evt);
            }
        });

        jLabel1.setFont(new java.awt.Font("Dialog", 0, 12));
        jLabel1.setText("Espressione:");

        textEspressione.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyTyped(java.awt.event.KeyEvent evt) {
                textEspressioneKeyTyped(evt);
            }
        });

        jLabel2.setFont(new java.awt.Font("Dialog", 0, 12));
        jLabel2.setText("Anteprima:");

        labelAnteprima.setBackground(new java.awt.Color(204, 255, 204));
        labelAnteprima.setBorder(javax.swing.BorderFactory.createEtchedBorder());

        btnRinomina.setText("Rinomina");
        btnRinomina.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                btnRinominaMouseClicked(evt);
            }
        });

        lista.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
        lista.addListSelectionListener(new javax.swing.event.ListSelectionListener() {
            public void valueChanged(javax.swing.event.ListSelectionEvent evt) {
                listaValueChanged(evt);
            }
        });

        jScrollPane1.setViewportView(lista);

        jLabel3.setFont(new java.awt.Font("Dialog", 0, 12));
        jLabel3.setText("Files da processare:");

        textInfo.setBackground(new java.awt.Color(204, 255, 204));
        textInfo.setEditable(false);
        textInfo.setFont(new java.awt.Font("Dialog", 0, 10));
        textInfo.setText("E' possibile definire delle espressioni che permettono una rinominazione automatica dei file. Gli operatori concessi sono <artista>, <titolo>, <album>, <commento>, <traccia>, <anno>, <genere>. Esempio: <traccia> - <artista> - <titolo>");
        jScrollPane2.setViewportView(textInfo);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 721, Short.MAX_VALUE)
                    .addComponent(jLabel1, javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(textEspressione, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 721, Short.MAX_VALUE)
                    .addComponent(labelAnteprima, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 721, Short.MAX_VALUE)
                    .addComponent(jLabel2, javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane2, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 721, Short.MAX_VALUE)
                    .addComponent(btnRinomina)
                    .addComponent(jLabel3, javax.swing.GroupLayout.Alignment.LEADING))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(textEspressione, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(1, 1, 1)
                .addComponent(jLabel2)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(labelAnteprima, javax.swing.GroupLayout.PREFERRED_SIZE, 17, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(btnRinomina)
                .addGap(8, 8, 8)
                .addComponent(jLabel3)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 201, Short.MAX_VALUE)
                .addContainerGap())
        );
        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void formWindowClosing(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowClosing
// TODO add your handling code here:
        MemoryCard.salva(lastEspr, textEspressione.getText());
    }//GEN-LAST:event_formWindowClosing
    
    private void aggiornaAnteprima(){
        AbstractCavaID3 mp3 = null;
        /*prende il primo o quello selezionato*/
        if (lista.getSelectedIndex() == -1){
            ListModel model = lista.getModel();
            mp3 = (AbstractCavaID3) model.getElementAt(0);
        } else {
            mp3 = (AbstractCavaID3) lista.getSelectedValue();
        }
        try{
            labelAnteprima.setText(new ID3FileNameHandler().parseRenamePattern(textEspressione.getText(), mp3));
        } catch (SintassiErrataException e){
            labelAnteprima.setText("Espressione scorretta");
        }
    }
    
    private void listaValueChanged(javax.swing.event.ListSelectionEvent evt) {//GEN-FIRST:event_listaValueChanged
// TODO add your handling code here:
        this.aggiornaAnteprima();
    }//GEN-LAST:event_listaValueChanged
    
    private void textEspressioneKeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_textEspressioneKeyTyped
// TODO add your handling code here:
        this.aggiornaAnteprima();
    }//GEN-LAST:event_textEspressioneKeyTyped
    
    private void btnRinominaMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_btnRinominaMouseClicked
        try {
            new ID3FileNameHandler().renameFiles(textEspressione.getText(), this.files);
        } catch (SintassiErrataException e) {
            JOptionPane.showMessageDialog(this, "Sintassi dell'espressione errata", "Errore sintassi", JOptionPane.WARNING_MESSAGE);
        }
        //aggiorna il form chiamante (i nomi dei vecchi file non valgono più) e se stesso
        this.lista.setListData(this.u.update());
    }//GEN-LAST:event_btnRinominaMouseClicked
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnRinomina;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JLabel labelAnteprima;
    private javax.swing.JList lista;
    private javax.swing.JTextField textEspressione;
    private javax.swing.JTextPane textInfo;
    // End of variables declaration//GEN-END:variables
    
}
